<?php

namespace App\Services\Huanxin;

class ChatRoomServices extends BaseService
{
    /**
     * 获取所有的聊天室
     * return $data
     */
    public function getAllChatRooms()
    {
        $url = $this->url . 'chatrooms';
        $header = array($this->getToken());
        $data = postCurl($url, '', $header, 'GET');
        return resultHuanxinInfo($data);
    }

    /**
     * 获取用户加入的聊天室
     * $username 用户环信ID
     * return $data
     */
    public function joinedChatRooms($username)
    {
        if (empty($username)) {
            return false;
        }
        $url = $this->url . 'users/' . $username . '/joined_chatrooms';
        $header = array($this->getToken());
        $data = postCurl($url, '', $header, 'GET');
        return resultHuanxinInfo($data);
    }

    /**
     * 获取聊天室详情
     * $chatroom_id 聊天室ID
     * return $data
     */
    public function getChatRoomsdetails($chatroom_id)
    {
        if (empty($chatroom_id)) {
            return false;
        }
        $url = $this->url . 'chatrooms/' . $chatroom_id;
        $header = array($this->getToken());
        $data = postCurl($url, '', $header, 'GET');
        return resultHuanxinInfo($data);
    }

    /**创建一个聊天室
     * @param $name聊天室名称，此属性为必须的
     * @param $description聊天室描述，此属性为必须的
     * @param $maxusers聊天室成员最大数（包括聊天室创建者），值为数值类型，此属性为可选的
     * @param $owner聊天室的管理员，此属性为必须的
     * @param $members    聊天室成员，此属性为可选的，但是如果加了此项，数组元素至少一个
     * @param string $type
     * @return $data
     */
    public function createChatRooms($name, $description, $maxusers, $owner, $members)
    {
        if (empty($name) || empty($description) || empty($maxusers) || empty($owner) || empty($members)) {
            return false;
        }
        $url = $this->url . 'chatrooms';
        $options = [
            'name' => $name,
            'description' => $description,
            'maxusers' => $maxusers,
            'owner' => $owner,
            'name' => [$members],
        ];
        $header = array($this->getToken());
        $body = json_encode($options);
        $data = postCurl($url, $body, $header, 'POST');
        return resultHuanxinInfo($data);
    }

    /**修改聊天室信息
     * @param $name聊天室名称，此属性为必须的
     * @param $description聊天室描述，此属性为必须的
     * @param $maxusers聊天室成员最大数（包括聊天室创建者），值为数值类型，此属性为可选的
     * @param $owner聊天室的管理员，此属性为必须的
     * @param $members    聊天室成员，此属性为可选的，但是如果加了此项，数组元素至少一个
     * @param string $type
     * @return $data
     */
    public function uptadeChatRooms($name, $description, $maxusers)
    {
        if (empty($name) || empty($description) || empty($maxusers) || empty($chatroom_id)) {
            return false;
        }
        $url = $this->url . 'chatrooms/' . $chatroom_id;
        $options = [
            'name' => $name,
            'description' => $description,
            'maxusers' => $maxusers,
        ];
        $header = array($this->getToken());
        $body = json_encode($options);
        $data = postCurl($url, $body, $header, 'PUT');
        return resultHuanxinInfo($data);
    }

    /**
     * 删除聊天室
     * $chatroom_id 聊天室ID
     * return $data
     */
    public function delChatRooms($chatroom_id)
    {
        if (empty($chatroom_id)) {
            return false;
        }
        $url = $this->url . 'chatrooms/' . $chatroom_id;
        $header = array($this->getToken());
        $data = postCurl($url, '', $header, 'DELETE');
        return resultHuanxinInfo($data);
    }

    /**
     * 分页获取聊天室成员
     * $chatroom_id 聊天室ID
     * return $data
     */
    public function getChatRoomsUser($chatroom_id)
    {
        if (empty($chatroom_id)) {
            return false;
        }
        $url = $this->url . 'chatrooms/' . $chatroom_id . '/users';
        $header = array($this->getToken());
        $data = postCurl($url, '', $header, 'GET');
        return resultHuanxinInfo($data);
    }

    /**
     * 添加单个聊天室成员
     * $chatroom_id 聊天室ID
     * $username  用户环信ID
     * return $data
     */
    public function addChatRoomsUser($chatroom_id, $username)
    {
        if (empty($chatroom_id) || empty($username)) {
            return false;
        }
        $url = $this->url . 'chatrooms/' . $chatroom_id . '/users/' . $username;
        $header = array($this->getToken());
        $data = postCurl($url, '', $header, 'POST');
        return resultHuanxinInfo($data);
    }

    /**
     * 删除单个聊天室成员
     * $chatroom_id 聊天室ID
     * $username  用户环信ID
     * return $data
     */
    public function delChatRoomsUser($chatroom_id, $username)
    {
        if (empty($chatroom_id) || empty($username)) {
            return false;
        }
        $url = $this->url . 'chatrooms/' . $chatroom_id . '/users/' . $username;
        $header = array($this->getToken());
        $data = postCurl($url, '', $header, 'DELETE');
        return resultHuanxinInfo($data);
    }

    /**
     * 获取聊天室管理员列表
     * $chatroom_id 聊天室ID
     * return $data
     */
    public function getChatRoomsAdmin($chatroom_id)
    {
        if (empty($chatroom_id)) {
            return false;
        }
        $url = $this->url . 'chatrooms/' . $chatroom_id . '/admin';
        $header = array($this->getToken());
        $data = postCurl($url, '', $header, 'GET');
        return resultHuanxinInfo($data);
    }

    /**
     * 添加聊天室管理员
     * $chatroom_id 聊天室ID
     * $username  用户环信ID
     * return $data
     */
    public function addChatRoomsAdmin($chatroom_id, $username)
    {
        if (empty($chatroom_id) || empty($username)) {
            return false;
        }
        $url = $this->url . 'chatrooms/' . $chatroom_id . '/admin';
        $options = [
            'newadmin' => $username
        ];
        $header = array($this->getToken());
        $body = json_encode($options);
        $data = postCurl($url, $body, $header, 'POST');
        return resultHuanxinInfo($data);
    }

    /**
     * 移除聊天室管理员
     * $chatroom_id 聊天室ID
     * $oldadmin  用户环信ID
     * return $data
     */
    public function delChatRoomsAdmin($chatroom_id, $oldadmin)
    {
        if (empty($chatroom_id) || empty($oldadmin)) {
            return false;
        }
        $url = $this->url . 'chatrooms/' . $chatroom_id . '/admin/' . $oldadmin;
        $header = array($this->getToken());
        $data = postCurl($url, '', $header, 'DELETE');
        return resultHuanxinInfo($data);
    }

    /**
     * 获取禁言列表
     * $chatroom_id 聊天室ID
     * return $data
     */
    public function getChatRoomsMute($chatroom_id)
    {
        if (empty($chatroom_id)) {
            return false;
        }
        $url = $this->url . 'chatrooms/' . $chatroom_id . '/mute';
        $header = array($this->getToken());
        $data = postCurl($url, '', $header, 'GET');
        return resultHuanxinInfo($data);
    }

    /**
     * 添加禁言
     * $chatroom_id 聊天室ID
     * $username  用户环信ID
     * $mute_duration 禁言的时间，单位毫秒，如果是“-1”代表永久（实际的到期时间为固定时间戳4638873600000，即2117-01-01 00:00:00）
     * return $data
     */
    public function addChatRoomsMute($chatroom_id, $username, $mute_duration)
    {
        if (empty($chatroom_id) || empty($username) || empty($mute_duration)) {
            return false;
        }
        $url = $this->url . 'chatrooms/' . $chatroom_id . '/mute';
        $header = array($this->getToken());
        $options = [
            'mute_duration' => $mute_duration,
            'usernames' => [$username],
        ];
        $body = json_encode($options);
        $data = postCurl($url, $body, $header, 'POST');
        return resultHuanxinInfo($data);
    }

    /**
     * 移除禁言
     * $chatroom_id 聊天室ID
     * $username  用户环信ID
     * $mute_duration 禁言的时间，单位毫秒，如果是“-1”代表永久（实际的到期时间为固定时间戳4638873600000，即2117-01-01 00:00:00）
     * return $data
     */
    public function delChatRoomsMute($chatroom_id, $username)
    {
        if (empty($chatroom_id) || empty($username)) {
            return false;
        }
        $url = $this->url . 'chatrooms/' . $chatroom_id . '/mute/' . $username;
        $header = array($this->getToken());
        $data = postCurl($url, '', $header, 'DELETE');
        return resultHuanxinInfo($data);
    }

    /**
     * 获取超级管理员列表
     * return $data
     */
    public function getChatRoomsSuperAdmin()
    {
        $url = $this->url . 'chatrooms/super_admin';
        $header = array($this->getToken());
        $data = postCurl($url, '', $header, 'GET');
        return resultHuanxinInfo($data);
    }

    /**
     * 添加超级管理员
     * $username 用户环信ID
     * return $data
     */
    public function addChatRoomsSuperAdmin($username)
    {
        if (empty($username)) {
            return false;
        }
        $url = $this->url . 'chatrooms/super_admin';
        $options = [
            'superadmin' => $username,
        ];
        $header = array($this->getToken());
        $body = json_encode($options);
        $data = postCurl($url, $body, $header, 'POST');
        return resultHuanxinInfo($data);
    }

    /**
     * 移除超级管理员
     * $username 用户环信ID
     * return $data
     */
    public function delChatRoomsSuperAdmin($username)
    {
        if (empty($username)) {
            return false;
        }
        $url = $this->url . 'chatrooms/super_admin/' . $username;
        $header = array($this->getToken());
        $data = postCurl($url, '', $header, 'DELETE');
        return resultHuanxinInfo($data);
    }

}
